package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 预约
 *
 * @author zz
 * @since 2022/11/23 8:42
 */
@Slf4j
@RestController
@RequestMapping("/mobile/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private MemberDao memberDao;

    @PostMapping("/submit")
    public Result submit(@RequestBody Map<Object,Object> map){
        String telephone = (String) map.get("telephone");
        String validateCode = (String) map.get("validateCode");
        String date =(String) map.get("orderDate");
        //String idCard = (String) map.get("idCard");
        //String sex = (String) map.get("sex");
        String s = (String) map.get("setmealId");
        Integer setmealId = Integer.valueOf(s);

        if (null== date){
            return new Result(false, "请选择预约时间");
        }

        Date orderDate = this.string2Date(date);
        Order order = new Order();
        order.setOrderDate(orderDate);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(setmealId);

        if (null== telephone){
            return new Result(false, "请输入手机号");
        }
        if (null== validateCode){
            return new Result(false, "请输入验证码");
        }

        Member member = memberDao.selectByTelephone(telephone);
        if (member == null){
            return new Result(false,"会员不存在");
        }
        Integer memberId = member.getId();
        order.setMemberId(memberId);

        String passcode = (String) redisTemplate.opsForValue().get(telephone);

        if (passcode==null || !passcode.equals(validateCode)){
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }

        Result submit = orderService.submit(order);
        redisTemplate.delete(telephone);
        return submit;

    }


    public Date string2Date(String date){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date orderDate = new Date();
        try {
            //使用SimpleDateFormat的parse()方法生成Date
            orderDate = new SimpleDateFormat("yyyy-MM-dd").parse(date);
            //打印Date
            System.out.println(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orderDate;
    }
}
